package com.uchengwang.ui.cm_ui.login.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.cycle.ui.ApplyTalentActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.InviteCodeCreateCycleActivity;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.cm_ui.ucity.cityBean.SchoolBean;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/2/7.
 */

public class SelectSchoolAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<SchoolBean> schools = new ArrayList<>();//学校
//    private List<AuthenStudentBean> majors = new ArrayList<>();//学院
    private Bundle bundle;
    private int type;//1注册是 完善信息选择学校后返回  2创建社圈时 没选择学校选择学校

    public SelectSchoolAdapter(Context context, int type){
        this.context = context;
        this.type = type;
    }

    public void setData(List<SchoolBean> schoolNames){
        this.schools = schoolNames;
        notifyDataSetChanged();
    }

    public List<SchoolBean> getData(){
        return schools;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_switch_school, parent, false);
        return new SelectVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        SelectVH holder = (SelectVH) h;

        final SchoolBean schoolBean = schools.get(position);
        if (!TextUtils.isEmpty(schoolBean.getUniArea())){
            holder.switch_name.setText(schoolBean.getUniName() +"("+schoolBean.getUniArea()+")");
        }else {
            holder.switch_name.setText(schoolBean.getUniName());
        }
         if (schoolBean.isHavaData()){
             holder.switch_school_tag.setVisibility(View.GONE);
         }else {
             holder.switch_school_tag.setVisibility(View.VISIBLE);
//             holder.switch_school_tag.setText("敬请期待...");
         }

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type){
                    case 1://1注册是 完善信息选择学校后返回
                        Intent intent = new Intent();
                        intent.putExtra(BaseConfig.UNI_ID, schoolBean.getId());
                        intent.putExtra(BaseConfig.UNI_NAME, schoolBean.getUniName());
                        ((Activity)context).setResult(0, intent);
                        ((Activity)context).finish();
                        break;
                    case 2://2创建社圈时 没选择学校选择学校  选择后直接进入创建社圈的界面
                        MapRequest map = new MapRequest();
                        map.put(BaseConfig.UNI_ID, schoolBean.getId());
                        map.put(BaseConfig.UNI_NAME, schoolBean.getUniName());
                        HttpUserManager.getManager().changeUserInfo((Activity) context, map, new OnResultListener() {
                            @Override
                            public void onResult(int stutas, String msg, int total, String data) {
                                if (stutas ==0){
                                    UChengApplication.myUniId = schoolBean.getId();
                                    UChengApplication.uniId = schoolBean.getId();
                                    UChengApplication.mySchoolName = schoolBean.getUniName();
                                    UChengApplication.schoolName = schoolBean.getUniName();

                                    SPUtils.getPreferences(context).putLong(BaseConfig.UNI_ID, schoolBean.getId());
                                    SPUtils.getPreferences(context).putString(BaseConfig.UNI_NAME, schoolBean.getUniName());
                                    context.startActivity(new Intent(context, InviteCodeCreateCycleActivity.class));
                                    ((Activity) context).finish();
                                }else {
                                    UCommonUtil.getUtil().showToast(context, "修改失败");
                                }
                            }
                        });
                        break;
                    case 3://3 申请达人时 没选择学校选择学校  选择后直接进入申请达人的界面
                        MapRequest mapRequest = new MapRequest();
                        mapRequest.put(BaseConfig.UNI_ID, schoolBean.getId());
                        mapRequest.put(BaseConfig.UNI_NAME, schoolBean.getUniName());
                        HttpUserManager.getManager().changeUserInfo((Activity) context, mapRequest, new OnResultListener() {
                            @Override
                            public void onResult(int stutas, String msg, int total, String data) {
                                if (stutas ==0){
                                    UChengApplication.myUniId = schoolBean.getId();
                                    UChengApplication.uniId = schoolBean.getId();
                                    UChengApplication.mySchoolName = schoolBean.getUniName();
                                    UChengApplication.schoolName = schoolBean.getUniName();

                                    SPUtils.getPreferences(context).putLong(BaseConfig.UNI_ID, schoolBean.getId());
                                    SPUtils.getPreferences(context).putString(BaseConfig.UNI_NAME, schoolBean.getUniName());
                                    context.startActivity(new Intent(context, ApplyTalentActivity.class));
                                    ((Activity) context).finish();
                                }else {
                                    UCommonUtil.getUtil().showToast(context, "修改失败");
                                }
                            }
                        });
                        break;
                }

            }
        });
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return schools.size();
    }

    class SelectVH extends RecyclerView.ViewHolder{
        private TextView switch_name, switch_school_tag;
        private View view;

        public SelectVH(View itemView) {
            super(itemView);

            AutoUtils.autoSize(itemView);
            view = itemView;
            switch_name = (TextView) itemView.findViewById(R.id.switch_school_name);
            switch_school_tag   = (TextView) itemView.findViewById(R.id.switch_school_tag);
        }
    }
}
